package com.manager.system.controller;

import com.manager.common.annotation.AspectLogInfo;
import com.manager.common.constant.AspectMethodOperationEnum;
import com.manager.common.constant.OperationLogEnumType;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.System_Organization;
import com.manager.model.System_Permission;
import com.manager.system.service.OrganizationService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/org")
public class OrganizationController  {


	@Autowired
	private OrganizationService organizationService;
	
	@RequestMapping("/getOrgPage")
	public PageVO getOrgPage(System_Organization model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		PageVO pageVO = organizationService.getOrgMap(model);
		return pageVO;
	}
	
	@RequestMapping("/getOrgList")
	public ResultVO getOrgList(HttpServletRequest request, HttpServletResponse response) throws IOException{
		try{
			JSONArray jsonArray = organizationService.getOrgList();
			return ResultVO.success(jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
	
	@RequestMapping("/addOrUpdate")
	@AspectLogInfo(type= AspectMethodOperationEnum.SAVE,tableName="tb_sys_org", content="添加或者修改机构信息", logType = OperationLogEnumType.ORG)
	public ResultVO addOrUpdate(System_Organization model,HttpServletResponse response,HttpServletRequest request) throws IOException{
		try{
			if(StringUtils.isNoneEmpty(model.getId())){
				organizationService.updateOrg(model);
			}else{
				organizationService.addOrg(model);
			}
			return ResultVO.success();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
	
	@RequestMapping("/deleteOrg")
	@AspectLogInfo(type= AspectMethodOperationEnum.DELETE,tableName="tb_sys_org", content="删除机构信息",  logType = OperationLogEnumType.ORG)
	public ResultVO deleteOrg(System_Organization model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		try{
			organizationService.deleteOrg(model);
			return ResultVO.success();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
	
	@RequestMapping("/getMenuTree")
	public ResultVO getMenuTree(HttpServletResponse response,System_Organization model) throws IOException{
		JSONArray jsonArray = organizationService.getMenuTree(model);
		return ResultVO.success(jsonArray);
	}

	@RequestMapping("/getPermissionsByOrgId")
	public ResultVO getPermissionsByOrgId(HttpServletResponse response,System_Organization model) throws IOException{
		try{
			List<System_Permission> list = organizationService.getPermissionsByOrgId(model.getId());
			return ResultVO.success(ObjectUtils.getJSONArray(System_Permission.config,list));
		}catch (Exception e){

		}
		return ResultVO.failed();
	}

	@RequestMapping("/getAllOrg")
	public ResultVO getAllOrg(HttpServletResponse response,System_Organization model) throws IOException{
		try{
			List<System_Organization> list = organizationService.getAllOrg();
			return ResultVO.success(ObjectUtils.getJSONArray(System_Organization.config,list));
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
}
